//
//  ZMContactVC.m
//  testDemo
//
//  Created by 赵林瑞 on 16/5/12.
//  Copyright © 2016年 RZOL. All rights reserved.
//

#import "ZMContactVC.h"

// 跳转到通讯录
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

@interface ZMContactVC ()<ABPeoplePickerNavigationControllerDelegate>

@end

@implementation ZMContactVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentViewController:peoplePicker animated:YES completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    
    
    
    ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    
    CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);
    
    //电话号码
    
    CFStringRef telValue = ABMultiValueCopyValueAtIndex(valuesRef,index);
    
    
    
    //读取firstname
    
    //获取个人名字（可以通过以下两个方法获取名字，第一种是姓、名；第二种是通过全名）。
    
    //第一中方法
    
    //    CFTypeRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    
    //    CFTypeRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
    
    //    //姓
    
    //    NSString * nameString = (__bridge NSString *)firstName;
    
    //    //名
    
    //    NSString * lastString = (__bridge NSString *)lastName;
    
    //第二种方法：全名
    
    CFStringRef anFullName = ABRecordCopyCompositeName(person);
    
    
    [self dismissViewControllerAnimated:YES completion:^{
        
//        self.telLabel.text = (__bridge NSString *)telValue;
        
//        self.nameLabel.text = [NSString stringWithFormat:@"%@%@",nameString,lastString];
        
//        self.nameLabel.text = [NSString stringWithFormat:@"%@",anFullName];
        
        NSLog(@"%@", (__bridge NSString *)telValue);
        NSLog(@"%@", [NSString stringWithFormat:@"%@",anFullName]);
        
        
    }];
    
}

@end
